﻿@inject IHttpService _httpService

@inject IUserService _userService
@inject NavigationManager NavigationManager
@inject IDataCacheService _dataCacheService
@inject IPopupService PopupService

@if (IsHidden == false)
{
    @if (Template == EntryStyleTemplate.Default)
    {
        <CnGalWebSite.Components.Buttons.MasaButton Rounded Icon="@(isBooking?IconType.Booking.ToIconString():IconType.UnBooking.ToIconString())" Text="@(isBooking?"已预约":"预约")" Class="@Class" OnClick="OnClick" />
    }
    else
    {
        <CnGalWebSite.Components.Buttons.MasaButton Rounded XLarge="@(_dataCacheService.IsApp?false:true)"  Icon="@(isBooking?IconType.Booking.ToIconString():IconType.UnBooking.ToIconString())" Text="@(isBooking?"已预约":"预约")" Class="@Class" OnClick="OnClick" />
    }
}
<CnGalWebSite.Components.Verifications.DeviceIdentification @ref="deviceIdentification" />

@code {
    [Parameter]
    public int Id { get; set; }

    [Parameter]
    public string Class { get; set; }
    [Parameter]
    public EntryStyleTemplate Template { get; set; }
    [Parameter]
    public bool IsHidden { get; set; }

    bool isBooking;
    public bool IsBooking { get { return isBooking; } }
    bool isReady = false;

    [CascadingParameter]
    private Task<AuthenticationState> authenticationStateTask { get; set; }
    [CascadingParameter]
    public ErrorHandler ErrorHandler { get; set; }
    CnGalWebSite.Components.Verifications.DeviceIdentification deviceIdentification;

    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
        var user = await authenticationStateTask;
        if (Id != 0 && isReady == false && user.User.Identity.IsAuthenticated)
        {
            try
            {
                //获取文章信息
                var AsyncModel = await _httpService.GetAsync<BookingGameStateModel>("api/booking/GetBookingGameState/" + Id);
                isBooking = AsyncModel.IsBooking;
                isReady = true;
                StateHasChanged();
            }
            catch (Exception ex)
            {
                //await ErrorHandler.ProcessError(ex, "判断用户是否预约游戏失败");
            }
        }

    }


    public async Task OnClick()
    {
        //判断用户是否登录
        var user = await authenticationStateTask;
        if (user.User.Identity.IsAuthenticated)
        {
            if (isBooking)
            {
                await UnThumbsUp();
            }
            else
            {
                await ThumbsUp();
            }
        }
        else
        {
            _userService.Login();
        }

    }

    private async Task UnThumbsUp()
    {
        var confirmed = await PopupService.ConfirmAsync("取消预约", "确定要取消预约吗？", AlertTypes.Info);
        if(confirmed==false)
        {
            return;
        }

        try
        {
            var obj = await _httpService.PostAsync<BookingGameModel, Result>("api/booking/BookingGame", new BookingGameModel { GameId = Id, IsBooking = false, Identification = await deviceIdentification.GetDeviceIdentificationModel() });
            //判断结果
            if (obj.Successful == false)
            {
                await PopupService.ToastAsync($"取消预约游戏失败{(string.IsNullOrWhiteSpace(obj.Error)?null:obj.Error)}", AlertTypes.Error);
            }
            else
            {
                await PopupService.ToastAsync($"成功取消预约游戏", AlertTypes.Success);
                isBooking = false;
                StateHasChanged();
            }
        }
        catch (Exception ex)
        {
            await ErrorHandler.ProcessError(ex, "取消预约游戏失败");
        }
    }
    private async Task ThumbsUp()
    {
        try
        {
            var obj = await _httpService.PostAsync<BookingGameModel, Result>("api/booking/BookingGame", new BookingGameModel { GameId = Id, IsBooking = true, Identification = await deviceIdentification.GetDeviceIdentificationModel() });
            //判断结果
            if (obj.Successful == false)
            {
                await PopupService.ToastAsync($"预约游戏失败{(string.IsNullOrWhiteSpace(obj.Error) ? null : obj.Error)}", AlertTypes.Error);
            }
            else
            {
                await PopupService.ToastAsync($"成功预约游戏", AlertTypes.Success);
                isBooking = true;
                StateHasChanged();
            }
        }
        catch (Exception ex)
        {
            await ErrorHandler.ProcessError(ex, "预约游戏失败");
        }
    }

}
